package com.web.annotation.untils;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class AnnotationUtils {
	
  public static <T extends Annotation> T getAnnotation(AnnotatedElement annotatedElement, Class<T> targetAnnotation)
  {
    return annotatedElement.getAnnotation(targetAnnotation);
  }
  
  public static Object getAttributeValue(Annotation annotation, String attributeName)
  {
    try
    {
      Method valueMethod = annotation.annotationType().getMethod(attributeName, (Class[])null);
      return valueMethod.invoke(annotation, (Object[])null);
    }
    catch (Exception e)
    {
      throw new IllegalArgumentException("Cannot get value from [" + attributeName + "] element of [" + annotation.annotationType() + "]", e);
    }
  }
  
  public static List<Method> getMethods(Class<?> typeClass)
  {
	  List<Method> results = new ArrayList();
    Method[] methods = typeClass.getMethods();
    for (Method method : methods)
    {
      results.add(method);
    }
    return results;
  }
  
  
  public static List<Field> getDeclaredFields(Class<?> typeClass)
  {
    List results = new ArrayList();
    Field[] fields = typeClass.getDeclaredFields();
    for (Field field : fields)
    {
      results.add(field);
    }
    return results;
  }
  
  
  public static List<Annotation> getAnnotations(AnnotatedElement annotatedElement)
  {
    List results = new ArrayList();
    Annotation[] annotations = annotatedElement.getAnnotations();
    if ((annotations != null) && (annotations.length > 0))
    {
      for (Annotation annotation : annotations)
      {
        results.add(annotation);
      }
    }
    return results;
  }
  
}
